package club.controller;

import club.pojo.Answer;
import club.pojo.User;
import club.service.AnswerService;
import club.util.Message;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;



@Controller
@RequestMapping("/answer")
public class AnswerController {

    @Resource
    private AnswerService answerService;


    @RequestMapping("/create")
    @ResponseBody
    public Message create(HttpSession session, Integer commentId, String content) {
        User user = (User) session.getAttribute("user");
        // 从会话中获取当前登录用户
        Integer integer = answerService.create(user.getId(), commentId, content);
        // 调用服务层创建回复
        if (integer > 0) {
            return Message.success();
        } else {
            return Message.fail();
        }
    }

    /**
     * 根据ID查找回复
     * @param id 回复ID
     * @return 包含回复信息的操作结果消息
     */
    @RequestMapping("/findById")
    @ResponseBody
    public Message findById(Integer id) {
        Answer answer = answerService.findById(id);
        // 调用服务层查找回复
        return Message.success().add("answer", answer);
    }

    /**
     * 创建回复（回复他人的回复）
     * @param session HTTP会话，用于获取当前登录用户
     * @param replayId 被回复的回复ID
     * @param content 回复内容
     * @param commentId 所属评论ID
     * @return 操作结果消息
     */
    @RequestMapping("/creates")
    @ResponseBody
    public Message creates(HttpSession session, Integer replayId, String content, Integer commentId) {
        User user = (User) session.getAttribute("user");
        // 从会话中获取当前登录用户
        Integer integer = answerService.creates(user.getId(), replayId, content, commentId);
        // 调用服务层创建回复
        if (integer > 0) {
            return Message.success();
        } else {
            return Message.fail();
        }
    }
}